package com.mjh.zhujianju.service.agency.impl;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import javax.annotation.Resource;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import com.google.common.collect.Lists;
import com.livew.framework.base.model.FileEO;
import com.livew.framework.base.model.SysUser;
import com.livew.framework.base.service.FileService;
import com.livew.framework.base.service.SysUserService;
import com.livew.framework.base.service.impl.BaseServiceImpl;
import com.livew.framework.exception.XTBusinessException;
import com.livew.framework.utils.DeleteType;
import com.livew.framework.utils.XTCurrentUser;
import com.livew.framework.utils.XTListUtils;
import com.mjh.zhujianju.dao.agency.DeptUserMapper;
import com.mjh.zhujianju.model.agency.DeptUserEO;
import com.mjh.zhujianju.model.base.UserExtendEO;
import com.mjh.zhujianju.service.agency.DeptUserService;
import com.mjh.zhujianju.service.base.UserExtendService;
import com.mjh.zhujianju.util.FileUrlAdapter;
import com.mjh.zhujianju.vo.agency.DeptUserVO;
import com.mjh.zhujianju.vo.base.SysUserVO;

/**
 * @author liuyang
 * @Description: 部门用户关系
 * @date 2018/8/8 22:58
 */
@Service
public class DeptUserServiceImpl extends BaseServiceImpl<DeptUserEO> implements DeptUserService {
	@Autowired
	private DeptUserMapper deptUserMapper;

	@Autowired
	private SysUserService sysUserService;

	@Autowired
	private UserExtendService userExtendService;

	@Resource
	private FileService fileService;
	
	public DeptUserServiceImpl(DeptUserMapper deptUserMapper) {
		super(deptUserMapper);
	}

	@Override
	public List<SysUserVO> queryDeptUserList(Long deptId) {
		List<SysUserVO> list = Lists.newArrayList();
		DeptUserEO deptUserEO = new DeptUserEO();
		deptUserEO.setDeptId(deptId);
		// 查询部门-人员 关系 List
		List<DeptUserEO> deptUserEOList = deptUserMapper.selectListByCondition(deptUserEO);
		if (CollectionUtils.isEmpty(deptUserEOList)) {
			return list;
		}

		// 获取所有userId
		List<Long> userIds = deptUserEOList.stream().map(deptUserEO1 -> deptUserEO1.getUserId()).distinct()
				.collect(Collectors.toList());

		// 查询用户信息
		List<SysUser> sysUserList = (List<SysUser>) sysUserService.getSysUserByIds(userIds);
		if (CollectionUtils.isEmpty(sysUserList)) {
			return list;
		}

		// 拼装用户
		sysUserList.stream().forEach(sysUser -> {
			SysUserVO sysUserVO = new SysUserVO();
			BeanUtils.copyProperties(sysUser, sysUserVO);
			list.add(sysUserVO);
		});

		// 组装图片
		List<FileEO> fileEOList = fileService.getFileByIds(userIds, SysUser.class.getName(), null);
		if (!CollectionUtils.isEmpty(fileEOList)) {
			list.stream().forEach(sysUserVO -> {
				fileEOList.stream().forEach(fileEO -> {
					if (sysUserVO.getId().equals(fileEO.getCaseId())) {
						sysUserVO.setFileId(fileEO.getId());
						sysUserVO.setFileAction(FileUrlAdapter.assemblyAction(fileEO.getId()));
					}
				});
			});
		}

		// 查询用户党员信息
		List<UserExtendEO> userExtendEOList = userExtendService.queryUserExtendByUserIds(userIds);
		if (CollectionUtils.isEmpty(userExtendEOList)) {
			return list;
		}

		// 拼装党员
		list.stream().forEach(sysUserVO -> {
			userExtendEOList.stream().forEach(userExtendEO -> {
				if (sysUserVO.getId().equals(userExtendEO.getUserId())) {
					sysUserVO.setUserExtendEO(userExtendEO);
				}
			});
		});
		return list;
	}

	@Override
	public SysUserVO querySysUserInfo(Long userId) throws XTBusinessException {

		SysUser user = sysUserService.getByPrimaryKey(userId);
		if (user == null) {
			return null;
		}
		SysUserVO sysUserVO = new SysUserVO();
		BeanUtils.copyProperties(user, sysUserVO);

		// 图片
		List<Long> fileIds = fileService.getFileByCaseId(sysUserVO.getId(), SysUser.class.getName(), null);
		if (!CollectionUtils.isEmpty(fileIds)) {
			sysUserVO.setFileId(fileIds.get(0));
		}

		// 党员信息
		List<Long> userIds = Lists.newArrayList(Arrays.asList(userId));
		List<UserExtendEO> userExtendEOList = userExtendService.queryUserExtendByUserIds(userIds);
		if (!CollectionUtils.isEmpty(userExtendEOList)) {
			sysUserVO.setUserExtendEO(userExtendEOList.get(0));
		}
		return sysUserVO;
	}

	@Override
	public List<Long> queryDeptUserByDeptId(Long deptId) {
		List<Long> userIds = Lists.newArrayList();
		DeptUserEO deptUserEO = new DeptUserEO();
		deptUserEO.setDeptId(deptId);
		List<DeptUserEO> deptUserEOList = deptUserMapper.selectListByCondition(deptUserEO);
		if (CollectionUtils.isEmpty(deptUserEOList)) {
			return userIds;
		}
		userIds = deptUserEOList.stream().map(deptUserEO1 -> deptUserEO1.getUserId()).distinct()
				.collect(Collectors.toList());
		return userIds;
	}

	@Override
	public Map<Long, DeptUserVO> getDeptByUserIds(List<Long> userIds) {
		if (XTListUtils.isEmpty(userIds)) {
			return null;
		}
		List<DeptUserVO> duVO = deptUserMapper.selectDeptByUserIds(userIds);
		if (XTListUtils.isEmpty(duVO)) {
			return null;
		}
		Map<Long, DeptUserVO> map = new HashMap<>();
		for (DeptUserVO deptUserVO : duVO) {
			map.put(deptUserVO.getUserId(), deptUserVO);
		}
		return map;
	}

	@Override
	public DeptUserVO getDeptByUserId(Long userId) {
		ArrayList<Long> userIds = new ArrayList<>();
		userIds.add(userId);
		List<DeptUserVO> list = deptUserMapper.selectDeptByUserIds(userIds);	
		if(XTListUtils.isEmpty(list)){
			return null;
		}
		return list.get(0);
	}

	@Override
	public int saveDeptUsers(List<DeptUserEO> deptUserEOS) {
		return deptUserMapper.insertDeptUsers(deptUserEOS);
	}

	@Override
	public void delDeptByUserId(Long userId) throws XTBusinessException {
		DeptUserEO deptUserEO = new DeptUserEO();
		deptUserEO.setUserId(userId);
		deptUserEO.setUpdateTime(new Date());
		deptUserEO.setUpdateUserId(XTCurrentUser.getCurrentUserId());
		deptUserEO.setIsDel(DeleteType.IS_DELETE);
		deptUserMapper.updateByPrimaryKeySelective(deptUserEO);
	}


	@Override
	public List<DeptUserEO> selectDeptByDeptIds(List<Long> list) {
		return deptUserMapper.selectDeptByDeptIds(list);
	}
	@Override
	public List<DeptUserVO> queryDeptListByDeptId(Long deptId) {
		List<DeptUserVO> list =	deptUserMapper.selectDeptListByDeptId(deptId);
		return list;
	}


}
